<?php

namespace app\admin\controller\service_config;

use app\common\model\admin\service_config\ServiceConfigRule;
use app\common\model\admin\service_config\ServiceConfigTab;
use think\admin\Controller;
use think\admin\helper\QueryHelper;

/**
 *  系统配置规则
 * @class ConfigRule
 * @package app\admin\controller\service_config
 * @Author: shirukun
 * @Date: 2024/11/7 17:14
 */
class ConfigRule extends Controller
{

    /**
     * 默认检测登陆后台用户
     *
     * @var bool
     */
    public $check_user = false;
    
    /**
     *  系统配置规则
     * @auth true
     * @menu true
     */
    public function index()
    {
        ServiceConfigRule::mQuery()->layTable(function () {
            $this->title = lang('配置规则');
            // $this->types = ServiceConfigTab::$types;
            $this->types = array_map('lang', ServiceConfigTab::$types);

            $this->type = $this->get['type'] ??  key($this->types);
            $group = ServiceConfigTab::mk()->where(['status' => 1])->field('type,name,code')->select()->toArray();
            $this->group = [];
            foreach ($group as $item){
                $this->group[$item['type']][] = $item;
            }

        }, function (QueryHelper $query) {
            $query->order('sort desc');
            // 数据列表搜索过滤
            $query->equal('code,group,type,status')->like('name#name')->dateBetween('create_time');
        });
    }

    protected function _index_page_filter(&$data)
    {
        $formTypeItems = ServiceConfigRule::formTypeItems();
        foreach ($data as &$v){
//            $v['form_type'] = _lang($formTypeItems[$v['form_type']] ?? '-');
            $v['form_type'] = $formTypeItems[$v['form_type']] ?? '-';
        }
    }

    /**
     * 添加配置参数
     * @auth true
     */
    public function add()
    {
//        $this->_applyFormToken();
        ServiceConfigRule::mForm('form');
    }

    /**
     * 编辑配置参数
     * @auth true
     */
    public function edit()
    {
//        $this->_applyFormToken();
        ServiceConfigRule::mForm('form');
    }

    /**
     * 修改数据状态
     * @auth true
     */
    public function state()
    {
        ServiceConfigRule::mSave($this->_vali([
            'status.in:1,0'  => lang('状态值范围异常'),
            'status.require' => lang('状态值不能为空'),
        ]));
    }

    /**
     * 删除配置参数
     * @auth true
     */
    public function remove()
    {
        ServiceConfigRule::mDelete();
    }

    /**
     * 表单数据处理
     * @param array $vo
     * @throws \ReflectionException
     */
    protected function _form_filter(array &$vo)
    {
        if ($this->request->isGet()) {
            // $this->types = ServiceConfigTab::$types;
           $this->types = array_map('lang', ServiceConfigTab::$types);
            $this->type = $this->get['type'];
            $this->formTypeItems = ServiceConfigRule::formTypeItems();
//            $this->formTypeItems = array_map(function ($item){
//                return _lang($item);
//            },ServiceConfigRule::formTypeItems());
            $this->group = ServiceConfigTab::mk()->where(['type' => $this->type])->column('name','code');
        }else{
            $param = $this->request->param();
            if (!isset($param['id'])){
                $res = ServiceConfigRule::mk()->where(['type' => $param['type'],'group' => $param['group'],'code' => $param['code']])->find();
                if ($res){
                    return $this->error(lang('此分组参数变量已存在'));
                }
            }
        }
    }
}